package com.msconfig.mvp.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.device.GeoLocationData;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.DoubleToLocationUtil;
import com.msconfig.utils.GeneralUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SatelliteLocateInformationFragment extends BaseMvpFragment implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private TextView tvSatelliteNum;
    private TextView tvSolutionState;
    private TextView tvDelayTime;
    private TextView tvLongitude;
    private TextView tvLatitude;
    private TextView tvAltitude;
    private TextView tvHrms;
    private TextView tvVrms;
    private TextView tvPdop;
    private TextView tvHdop;
    private TextView tvVdop;
    private TextView tvTime;
    private String strHour;
    private String strMinu;
    private String strSec;
    private GeoLocationData geoLocationData;

    //定位信息更新
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showUpdateLocationResultStatus(DeviceUpdateEvent.UpdateLocationResultStatus event) {
        showReceiveLocationData();
    }

    private void showReceiveLocationData() {
        int totalSatelites = (int) geoLocationData.getSateInfoList().getSateInfoList().size();
        int userSatelites = geoLocationData.getSateInfoList().getGpsInLock() + geoLocationData.getSateInfoList().getGlonassInLock()
                + geoLocationData.getSateInfoList().getGaileoInLock() + geoLocationData.getSateInfoList().getBdInLock();
        tvSatelliteNum.setText(getString(R.string.satellite_num, userSatelites,totalSatelites));
        // 质量因子——0=未定位，1=GPS单点定位固定解，2=差分定位，3=PPS解；4=RTK固定解；5=RTK浮点解；6=估计值；
        // 7=手工输入模式；8=模拟模式；
        String name = "";
        switch (geoLocationData.getSolutionType().swigValue()){
            case -1:
                name=getString(R.string.text_quality_indicator_no_data);
                break;
            case 0:
                name=getString(R.string.text_quality_indicator_no_location);
                break;
            case 1:
                name=getString(R.string.text_quality_indicator_single);
                break;//Differential positioning
            case 2:
                name=getString(R.string.text_quality_indicator_diff_posi);
                break;
            case 3:
                name=getString(R.string.text_quality_indicator_pps);
                break;
            case 4:
                name=getString(R.string.text_quality_indicator_rtk_fix);
                break;
            case 5:
                name=getString(R.string.text_quality_indicator_rtk_float);
                break;
            case 6:
                name=getString(R.string.text_quality_indicator_rtk_estimate);
                break;
            case 7:
                name=getString(R.string.text_quality_indicator_rtk_input);
                break;
            case 8:
                name=getString(R.string.text_quality_indicator_simulation);
                break;

        }
        tvSolutionState.setText(name);
        tvDelayTime.setText(geoLocationData.getAgeOfDiff()+"s");
        tvLongitude.setText(DoubleToLocationUtil.dblToLocation(geoLocationData.getLongitude()));
        tvLatitude.setText(DoubleToLocationUtil.dblToLocation(geoLocationData.getLatitude()));
        tvAltitude.setText(String.valueOf(GeneralUtil.retain3Point((float)geoLocationData.getAltitude())));
        //VRMS,HRMS
        tvVrms.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getVrms())));
        tvHrms.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getHrms())));
        //pdop,hdop,vdop
        tvPdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getPdop())));
        tvHdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getHdop())));
        tvVdop.setText(String.valueOf(GeneralUtil.retain3Point(geoLocationData.getVdop())));
        if(geoLocationData.getLacalTime().getHour()<10 ){
            strHour=getString(R.string.time_3,geoLocationData.getLacalTime().getHour());
        }else{
            strHour= String.valueOf(geoLocationData.getLacalTime().getHour());
        }
        if(geoLocationData.getLacalTime().getMinute()<10 ){
            strMinu=getString(R.string.time_3,geoLocationData.getLacalTime().getMinute());
        }else{
            strMinu= String.valueOf(geoLocationData.getLacalTime().getMinute());
        }
        if(geoLocationData.getLacalTime().getSecond()<10 ){
            strSec=getString(R.string.time_3,geoLocationData.getLacalTime().getSecond());
        }else{
            strSec= String.valueOf(geoLocationData.getLacalTime().getSecond());
        }
        tvTime.setText(getString(R.string.local_time,geoLocationData.getLacalTime().getYear(),geoLocationData.getLacalTime().getMonth(),geoLocationData.getLacalTime().getDay(),strHour,strMinu,strSec));
    }

    public static SatelliteLocateInformationFragment newInstance() {
        Bundle args = new Bundle();
        SatelliteLocateInformationFragment fragment = new SatelliteLocateInformationFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_satellite_lacate_information;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        tvSatelliteNum = (TextView)view.findViewById( R.id.tv_satellite_num );
        tvSolutionState = (TextView)view.findViewById( R.id.tv_mcu_firmware_version);
        tvDelayTime = (TextView)view.findViewById( R.id.tv_delay_time);
        tvLongitude = (TextView)view.findViewById( R.id.tv_longitude );
        tvLatitude = (TextView)view.findViewById( R.id.tv_latitude );
        tvAltitude = (TextView)view.findViewById( R.id.tv_altitude );
        tvHrms = (TextView)view.findViewById( R.id.tv_hrms );
        tvVrms = (TextView)view.findViewById( R.id.tv_vrms );
        tvPdop = (TextView)view.findViewById( R.id.tv_pdop );
        tvHdop = (TextView)view.findViewById( R.id.tv_hdop );
        tvVdop = (TextView)view.findViewById( R.id.tv_vdop );
        tvTime = (TextView)view.findViewById( R.id.tv_time );

    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_locate_info);
        geoLocationData = GeoLocationData.getInstance();
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }
    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }
}
